package com.jiantong.work.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.*;
import com.jiantong.work.impl.dao.*;
import com.jiantong.work.model.*;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by niufei on 2017/3/20.
 */
@Component
public class SupervisionRecordsManager {

    private final SupervisionRecordsDao supervisionRecordsDao;

    private final ReportHandleResumeDao reportHandleResumeDao;

    @Autowired
    public SupervisionRecordsManager(
            SupervisionRecordsDao supervisionRecordsDao,
            ReportHandleResumeDao reportHandleResumeDao) {

        this.supervisionRecordsDao = supervisionRecordsDao;
        this.reportHandleResumeDao = reportHandleResumeDao;
    }

    /**
     * 督查轨迹--政府端新建单据
     * @param supervisionRecords 督查轨迹的对象
     * @param toStatus 到状态
     * @return Map<String ,Object>
     */
    @Transactional
    public Map<String , Object> createSupervisionRecords(SupervisionRecords supervisionRecords, Integer toStatus){
        Integer fromStatus = supervisionRecords.getStatus();
        ReportHandleResume reportHandleResume ;
        Map<String , Object> map = Maps.newHashMap();

        LoginUser loginUser = UserUtil.getCurrentUser();
        String prefix = "JC";
        prefix = Utils.createPrefix(prefix,loginUser);//单据编号前缀的开头字母 和 当前用户
        String number = Utils.create(prefix,supervisionRecordsDao.findCurrentMaxNumber(prefix));//要在表中拿到当前最大的Number号
        supervisionRecords.setNumber(number);
        supervisionRecords.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());

        //如果传入了下一个状态, 并且传入的轨迹对象现在的状态和下一个状态不相等那么,改变轨迹对象的状态,然后创建出来,把ID放入map. 记录本次操作并存放到记录表中
        if(toStatus != null && !fromStatus.equals(toStatus)){
            supervisionRecords.setStatus(toStatus);
            supervisionRecordsDao.create(supervisionRecords);
            map.put("supervisionRecordsId" ,supervisionRecords.getId());

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(supervisionRecords.getId());
            reportHandleResume.setReportNumber(supervisionRecords.getNumber());
            reportHandleResume.setType(DocumentsType.SUPERVISION_RECORD.value());
            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);

            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId" ,reportHandleResume.getId());
            return map;
        }else{
            supervisionRecordsDao.create(supervisionRecords);
            map.put("supervisionRecordsId" ,supervisionRecords.getId());
            return map;
        }
    }

    /**
     * 督查轨迹--政府端更新单据
     * @param supervisionRecords 督查轨迹的对象
     * @param toStatus 到状态
     * @return Map<String ,Object>
     */
    @Transactional
    public Map<String , Object> updateSupervisionRecords(SupervisionRecords supervisionRecords , Integer toStatus){
        Integer fromStatus = supervisionRecords.getStatus();
        ReportHandleResume reportHandleResume ;
        Boolean flagBreedReport ;
        Map<String , Object> map = Maps.newHashMap();
        if(toStatus != null && !fromStatus.equals(toStatus)){
            supervisionRecords.setStatus(toStatus);
            flagBreedReport = supervisionRecordsDao.update(supervisionRecords);
            map.put("flagBreedReport",flagBreedReport);

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(supervisionRecords.getId());
            reportHandleResume.setReportNumber(supervisionRecords.getNumber());
            reportHandleResume.setType(DocumentsType.SUPERVISION_RECORD.value());
            reportHandleResume.setUserId(UserUtil.getUserId());
            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);

            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());
        }else{
            flagBreedReport = supervisionRecordsDao.update(supervisionRecords);
            map.put("flagBreedReport",flagBreedReport);
        }
        return map;
    }

}
